package com.xxh.search.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xxh.common.utils.Result;
import com.xxh.dataaccess.dao.UserDao;
import com.xxh.dataaccess.dao.VideoDao;
import com.xxh.dataaccess.dto.MovieDto;
import com.xxh.dataaccess.entity.User;
import com.xxh.dataaccess.entity.Video;
import com.xxh.dataaccess.page.PageResult;
import com.xxh.dataaccess.service.MovieService;
import com.xxh.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/search")
public class SearchController {

    @Autowired
    MovieService movieService;

    @Autowired
    VideoDao videoDao;

    @Autowired
    UserDao userDao;

    @Autowired
    SearchService searchservice;


    @GetMapping("/getRestult")
    public Result SearchRequest(@RequestParam(name = "keyword",required = true) String keyword,
                                @RequestParam(name = "type",required = true) String type,
                                @RequestParam(name = "cur", required = false, defaultValue = "1") Integer cur,
                                @RequestParam(name = "size", required = false, defaultValue = "10") Integer size) {

        com.baomidou.mybatisplus.extension.plugins.pagination.Page<Object> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(cur, size);
        IPage search = searchservice.search(page, keyword, type);
        return Result.success().put("data",search);
    }


    /**
     * 初始化ES的数据
     *
     * @return
     */
    @GetMapping("/initMovieES")
    public Result initEsMovieData() {
        int size = 100;
        for (int i = 1; i < 50; i++) {
            PageResult<MovieDto> result = movieService.PagingMovies(i, size);
            int insertedNum = searchservice.initMovieEsData(result.getList());
            if (insertedNum < size) {
                break;
            }
        }
        return Result.success();
    }

    @GetMapping("/initVideoES")
    public Result initEsVideoData() {
        int size = 100;
        for (int i = 1; i < 500; i++) {
            Page<Video> videos = videoDao.findAll(PageRequest.of(i, size));
            int insertedNum = searchservice.initVideoEsDate(videos.getContent());
            if (insertedNum < size) {
                break;
            }
        }
        return Result.success();
    }

    @GetMapping("/initUserES")
    public Result initEsUserData() {

        int size = 100;
        for (int i = 1; i < 500; i++) {
            com.baomidou.mybatisplus.extension.plugins.pagination.Page<User> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(i, size);
            IPage<User> selectPage = userDao.selectPage(page, null);
            List<User> users = selectPage.getRecords();
            int insertedNum = searchservice.initUserEsData(users);
            if (insertedNum < size) {
                break;
            }
        }
        return Result.success();
    }


}
